#
# BrowserItem.py
# (c) 2008 Will Baker.
#

from PyQt4.QtCore import Qt, QObject, SIGNAL, QSize, QRect, QPoint, QTimer

from model import ComicLoader

class BrowserItem:
    """
    An Item in a Browser.
    """
    size =  QSize( 100, 140 )

    def __init__( self, comic, viewer ):
        self.comic = comic
        self.viewer = viewer
        self.comic_loader = ComicLoader( self )
        self.page = None
        self.dirty = True
        self.loading = False
        self.scale = 1.0
        self.desired_scale = 1.0
        self.x = 0
        self.y = 0
        self.scale_timer = QTimer()
        
        QObject.connect( self.scale_timer, SIGNAL("timeout()"), self.on_update_scale )
    
    def refresh( self ):
        """
        Begin loading the cover of the comic in a thread.
        """
        if not self.loading and not self.page:
            self.loading = True
            self.comic_loader.load_single_page( self.comic, 0 )
        
    def on_page_count_loaded( self ):
        """
        Handle the page count being loaded. This is generally called inside a JobThread.
        """
        pass
        
    def on_page_loaded( self, page ):
        """
        Handle page data being loaded. This is generally called inside a JobThread.
        """
        self.dirty = False
        self.loading = False
        self.page = page
        self.viewer.update()
        
    def clear( self ):
        """
        Stop loading previews.
        """
        self.comic_loader.stop()
        
    def rect( self ):
        """
        Get the rectangle of this BrowserItem.
        """
        position = QPoint( self.x, self.y )
        offset = QPoint( (self.scale-1.0)/2 * self.size.width(), (self.scale-1.0)/2 * self.size.height() )
        return QRect( position - offset, self.scale * BrowserItem.size )
        return QRect( QPoint(self.x, self.y), self.scale * BrowserItem.size )
        
    def on_enter( self ):
        """
        Handle the mouse cursor entering this item.
        """
        self.desired_scale = 1.05
        self.scale_timer.start( 10 )
        
    def on_leave( self ):
        """
        Handle the mouse cursor leaving this item.
        """
        self.desired_scale = 1.0
        self.scale_timer.start( 10 )
        
    def on_update_scale( self ):
        """
        Update the scale of this BrowserItem so that it reflects the desired scale.
        """
        if self.scale < self.desired_scale:
            self.scale += 0.01
        elif self.scale > self.desired_scale:
            self.scale -= 0.01
        else:
            self.scale_timer.stop()
        
        self.viewer.update()
        
    def render( self, painter ):
        """
        Render this BrowserItem.
        """
        if self.dirty:
            self.refresh()
            
        if self.page:
            painter.drawPixmap( self.rect(), self.page.thumbnail )
        else:
            painter.fillRect( self.rect(), Qt.gray )
